import java.util.Arrays;

public class MinMoves2 {

    public int minMoves2(int[] nums) {
        int n = nums.length;
        Arrays.sort(nums);
        int target;
        if (n % 2 == 0) {
            target = (nums[n / 2] + nums[n / 2] - 1) / 2;
        } else {
            target = nums[n / 2];
        }
        int ans = 0;
        for (int i = 0; i < n; i++) {
            ans += Math.abs(nums[i] - target);
        }
        return ans;
    }
}
